package com.caojing.haolemai.ui.home;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseFragment;
import com.caojing.haolemai.bean.HomeLvBean;
import com.caojing.haolemai.bean.HomeVpBean;
import com.caojing.haolemai.ui.adapter.HomeLvAdapter;
import com.caojing.haolemai.ui.adapter.HomePagerAdapter;
import com.caojing.haolemai.ui.collect.CollectActivity;
import com.caojing.haolemai.ui.homedetail.HomeDetailActivity;
import com.caojing.haolemai.ui.main.MainActivity;
import com.caojing.haolemai.ui.search.SearchActivity;
import com.caojing.haolemai.view.MyListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements HomeContract.View {


    @Bind(R.id.home_fragment_search)
    LinearLayout homeFragmentSearch;
    @Bind(R.id.home_fragment_vp)
    ViewPager homeFragmentVp;
    @Bind(R.id.home_fragment_iv_brand)
    LinearLayout homeFragmentIvBrand;
    @Bind(R.id.home_fragment_iv_category)
    LinearLayout homeFragmentIvCategory;
    @Bind(R.id.home_fragment_iv_order)
    LinearLayout homeFragmentIvOrder;
    @Bind(R.id.home_fragment_iv_collect)
    LinearLayout homeFragmentIvCollect;
    @Bind(R.id.home_fragment_tv_comment)
    TextView homeFragmentTvComment;
    @Bind(R.id.home_fragment_lv)
    MyListView homeFragmentLv;
    @Bind(R.id.home_fragment_scrollview)
    ScrollView homeFragmentScrollview;
    @Bind(R.id.ll_points)
    LinearLayout llPoints;
    private HomePagerAdapter pagerAdapter;
    private HomePresenter presenter;
    private List<ImageView> ivlist;
    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onResume() {
        super.onResume();
        homeFragmentLv.setFocusable(false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {
        presenter = new HomePresenter(this);
        ivlist = new ArrayList<>();
    }

    @Override
    protected void initData() {
        presenter.initData();
    }


    @Override
    public void onSuccessGetHomeVpBean(HomeVpBean homeVpBean) {
        final List<HomeVpBean.ResultsBean> list = homeVpBean.getResults();
        for (int i =0;i<list.size();i++){
            ImageView iv = new ImageView(getContext());
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            Glide.with(this).load(list.get(i).getFocusImage()).thumbnail(0.1f).placeholder(R.mipmap.global_default_image_outlets).centerCrop().error(R.mipmap.global_default_image_outlets).into(iv);
            ivlist.add(iv);
            final int finalI = i;
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), HomeDetailActivity.class);
                    String contentId =""+ list.get(finalI).getFocusId();
                    intent.putExtra("contentId",contentId);
                    intent.putExtra("imageurl",list.get(finalI).getFocusImage());
                    startActivity(intent);
                }
            });
        }
        initDots();
        pagerAdapter = new HomePagerAdapter(ivlist);
        homeFragmentVp.setAdapter(pagerAdapter);
        initViewPagerAuto();
    }

    @Override
    public void onFailGetHomeVpBean(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccessGetHomeLvBean(final HomeLvBean homeLvBean) {
        HomeLvAdapter adapter = new HomeLvAdapter(homeLvBean.getResults(),getContext());
        homeFragmentLv.setAdapter(adapter);
        homeFragmentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(getActivity(), HomeDetailActivity.class);
                String contentId =""+ homeLvBean.getResults().get(position).getEventContent().getId();
                intent.putExtra("contentId",contentId);
                intent.putExtra("imageurl",homeLvBean.getResults().get(position).getAdvertPic());
                intent.putExtra("name",homeLvBean.getResults().get(position).getContents());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onFailGetHomeLvBean(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }


    @OnClick({R.id.home_fragment_search, R.id.home_fragment_iv_brand, R.id.home_fragment_iv_category, R.id.home_fragment_iv_order, R.id.home_fragment_iv_collect, R.id.home_fragment_tv_comment})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_fragment_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.home_fragment_iv_brand:
                Toast.makeText(getContext(), "功能重复", Toast.LENGTH_SHORT).show();
                break;
            case R.id.home_fragment_iv_category:
                MainActivity activity = (MainActivity) getActivity();
                activity.selectFragment(1);
                break;
            case R.id.home_fragment_iv_order:
                Toast.makeText(getContext(), "暂无订单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.home_fragment_iv_collect:
                startActivity(new Intent(getActivity(), CollectActivity.class));
                break;
            case R.id.home_fragment_tv_comment:
                break;
        }
    }

    /*初始化viewpager的小圆点*/
    private void initDots() {
        for (int i = 0;i<ivlist.size();i++){
            View points = new View(getContext());
            points.setBackgroundResource(R.mipmap.dot_white);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10,10);
            params.leftMargin = 15;
            points.setLayoutParams(params);
            llPoints.addView(points);
        }
        llPoints.getChildAt(0).setBackgroundResource(R.mipmap.dot_black);
    }
    /*viewpager自动无线轮播*/
    private void initViewPagerAuto() {
        homeFragmentVp.setCurrentItem(1000*ivlist.size());
        homeFragmentVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0;i<llPoints.getChildCount();i++){
                    llPoints.getChildAt(i).setBackgroundResource(R.mipmap.dot_white);
                    if ( i == position%ivlist.size()){
                        llPoints.getChildAt(i).setBackgroundResource(R.mipmap.dot_black);
                    }
                }
            }
        });
        Timer mTimer = new Timer();
        mTimer.schedule(new MyTimeTask(),2000);
    }
    /*定时器*/
    class  MyTimeTask extends TimerTask {
        private boolean isLoop = true;
        @Override
        public void run() {
            while (isLoop){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int currentItem =homeFragmentVp.getCurrentItem();
                        homeFragmentVp.setCurrentItem(++currentItem);
                    }
                });
            }
        }
    }

}
